<template>
    <view v-if="user.version">
        <st-uni-dynamic-form :form-items="userForm" @submit-form="submitForm"></st-uni-dynamic-form>
    </view>
</template>

<script>
    import StUniDynamicForm from '../../components/StUniDynamicForm'
    import {mapState} from 'vuex'

    export default {
        computed: mapState([
            'userInfo',
        ]),
        components: {StUniDynamicForm},
        data() {
            return {
                userForm: [
                    {
                        required: true,
                        eType: 'TEXT',
                        name: 'name',
                        label: '姓名'
                    },
                    {
                        required: true,
                        eType: 'TEXT',
                        name: 'nickname',
                        label: '昵称'
                    },
                    {
                        required: true,
                        eType: 'DATE_PICKER',
                        name: 'birthDay',
                        label: '生日',
                        type: 'date'
                    },
                    {
                        required: true,
                        eType: 'CHECKBOX_DICT',
                        name: 'genderDictId',
                        label: '性别',
                        type: 'gender'
                    },
                    {
                        required: true,
                        eType: 'DATA_PICKER',
                        name: 'maritalStatusDictId',
                        label: '婚姻状态',
                        type: 'married_status_parent'
                    },
                    {
                        required: true,
                        eType: 'TEXT',
                        name: 'mobile',
                        label: '手机号',
                        ruleType: 'mobile'
                    },
                    {
                        required: false,
                        eType: 'TEXT',
                        name: 'wechatNumber',
                        label: '微信号'
                    },
                    {
                        required: false,
                        eType: 'DATA_PICKER',
                        name: 'idTypeDictId',
                        label: '证件类型',
                        type: 'id_type_group'
                    },
                    {
                        required: false,
                        eType: 'TEXT',
                        name: 'idCardNo',
                        maxlength: 20,
                        label: '证件号'
                    },
                    {
                        required: true,
                        eType: 'NUMBER',
                        name: 'height',
                        min: 0,
                        max: 400,
                        label: '身高•cm',
                    },
                    {
                        required: true,
                        eType: 'NUMBER',
                        min: 0,
                        max: 500,
                        name: 'weight',
                        label: '体重•kg'
                    },
                    {
                        required: false,
                        eType: 'DATA_PICKER',
                        name: 'bloodTypeDictId',
                        label: '血型',
                        type: 'blood_type_parent'
                    },
                    {
                        required: true,
                        eType: 'DATA_PICKER',
                        name: 'drinkingDictId',
                        label: '喝酒',
                        type: 'drinking_status_parent'
                    },
                    {
                        required: true,
                        eType: 'DATA_PICKER',
                        name: 'smokingDictId',
                        label: '抽烟',
                        type: 'smoking_status_parent'
                    },
                    {
                        required: false,
                        eType: 'DATA_PICKER',
                        name: 'constellationDictId',
                        label: '星座',
                        type: 'constellation_type_parent'
                    },
                    {
                        required: true,
                        eType: 'DATA_PICKER',
                        name: 'nowAreaId',
                        label: '目前在',
                        type: 'area'
                    },
                    {
                        required: true,
                        eType: 'DATA_PICKER',
                        name: 'homeAreaId',
                        label: '家乡在',
                        type: 'area'
                    },
                    {
                        required: true,
                        eType: 'DATA_PICKER',
                        name: 'educationDictId',
                        label: '学历',
                        type: 'education_level_parent'
                    },
                    {
                        required: false,
                        eType: 'DATA_PICKER',
                        name: 'academicDictId',
                        label: '学位',
                        type: 'academic_level_parent'
                    },
                    {
                        required: false,
                        eType: 'TEXT',
                        name: 'college',
                        label: '学校'
                    },
                    {
                        required: false,
                        eType: 'TEXT',
                        name: 'major',
                        label: '专业'
                    },
                    {
                        required: false,
                        eType: 'TEXT',
                        name: 'profession',
                        label: '职业'
                    },
                    {
                        required: true,
                        eType: 'DATA_PICKER',
                        name: 'isHasCar',
                        label: '车',
                        type: 'isHas'
                    },
                    {
                        eType: 'TEXT',
                        name: 'carCity',
                        label: '车牌地',
                        placeholder: '如：京牌'
                    },
                    {
                        required: true,
                        eType: 'DATA_PICKER',
                        name: 'isHasHourse',
                        label: '房',
                        type: 'isHas'
                    },
                    {
                        eType: 'TEXT',
                        name: 'hourseCity',
                        label: '房所在地',
                        placeholder: '如：北京'
                    },
                    {
                        eType: 'TEXT',
                        name: 'monthSalary',
                        label: '月薪•万'
                    },
                    {
                        eType: 'TEXT',
                        name: 'yearSalary',
                        label: '年薪•万'
                    },
                    {
                        required: true,
                        eType: 'CHECKBOX_DICT',
                        name: 'isShowInList',
                        label: '首页展示',
                        type: 'isYesNo',
                        value: true
                    },
                    {
                        required: true,
                        eType: 'TEXT',
                        type: 'textarea',
                        name: 'description',
                        label: '个人简介',
                        maxlength: 300,
                        autoHeight: true
                    },
                    {
                        required: true,
                        eType: 'TEXT',
                        type: 'textarea',
                        name: 'standard',
                        label: '择偶标准',
                        maxlength: 300,
                        autoHeight: true
                    }
                ],
                user: {
                    id: null,
                    userId: null,
                    version: null
                }
            }
        },
        onLoad() {
        },
        mounted() {
            this.getDetail()
        },
        methods: {
            submitForm(data) {
                let formData = this.user
                Object.assign(formData, data)
                this.$http.put('/user-info', formData).then(() => {
                    uni.showModal({
                        title: '提示',
                        confirmText: '返回',
                        content: '保存成功！',
                        success: res => {
                            if (res.confirm) {
                                uni.switchTab({
                                    url: '/pages/user/user'
                                })
                            }
                        }
                    })
                }).catch(function (res) {
                    uni.showToast({
                        icon: 'none',
                        title: res.data.errorMsg
                    });
                })
            },
            async getDetail() {
              if(!this.userInfo.id){
                return;
              }
                uni.showLoading({
                    title: '加载中'
                })
                const {data: res} = await this.$http.get('/user-info/userId/' + this.userInfo.id)

                this.user.id = res.id
                this.user.userId = res.userId
                this.user.version = res.version
                let form = []
                Object.assign(form, this.userForm)

                form.forEach(e => {
                    e.value = res[e.name]
                })

                this.userForm = form
                uni.hideLoading()
            }
        },
        watch: {}
    }
</script>

<style>

</style>
